<script setup>
import { getData, saveData, findMaxSort } from "@/api/system/dict/data";

const props = defineProps({
  id: {
    type: [Number, String],
    default: ""
  },
  dictType: {
    default: String
  }
});

const formRef = ref();
const data = ref({
  loading: false,
  form: {
    id: props.id,
    dictType: props.dictType
  },
  rules: {
    dictLabel: [{ required: true, message: "请输入字典标签", trigger: "blur" }],
    dictValue: [{ required: true, message: "请输入字典键值", trigger: "blur" }]
  }
});

onMounted(() => {
  if (data.value.form.id != "") {
    getInfo();
  } else {
    findMaxSort(props.dictType).then((res) => {
      data.value.form.dictSort = res.data;
    });
  }
});

function getInfo() {
  data.value.loading = true;
  getData(props.id).then((res) => {
    data.value.loading = false;
    data.value.form = res.data;
  });
}

defineExpose({
  submit(callback) {
    if (data.value.form.id == "") {
      formRef.value.validate((valid) => {
        if (valid) {
          saveData(data.value.form).then(() => {
            ElMessage.success({
              message: "保存成功",
              center: true
            });
            callback && callback();
          });
        }
      });
    } else {
      formRef.value.validate((valid) => {
        if (valid) {
          saveData(data.value.form).then(() => {
            ElMessage.success({
              message: "修改成功",
              center: true
            });
            callback && callback();
          });
        }
      });
    }
  }
});
</script>

<template>
  <div v-loading="data.loading">
    <el-form
      ref="formRef"
      :model="data.form"
      :rules="data.rules"
      label-width="120px"
      label-suffix=" "
      label-position="top"
      style="padding: 0 20px"
    >
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="字典类型" prop="dictType">
            <el-input v-model="data.form.dictType" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="数据标签" prop="dictLabel">
            <el-input
              v-model="data.form.dictLabel"
              placeholder="请输入字典类型"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="数据键值" prop="dictValue">
            <el-input
              v-model="data.form.dictValue"
              placeholder="请输入字典类型"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序" prop="dictSort">
            <el-input v-model="data.form.dictSort" placeholder=""></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-radio-group
              v-model="data.form.status"
              placeholder="请选择字典类型"
            >
              <el-radio-button label="0" checked>正常</el-radio-button>
              <el-radio-button label="1">停用</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="data.form.remark"
              placeholder="备注信息"
              type="textarea"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
